﻿Imports DAO
Imports DTO
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.XtraTreeList
Imports System.ComponentModel

Public Class NhomNDBUS
    Private Shared dsQuyenNhomND As List(Of PhanQuyenNhonNDDTO)

    Shared Function layDsNhomND() As BindingList(Of NhomNDDTO)
        Dim dsND As BindingList(Of NguoiDungDTO) = NhomNDDAO.LayDSNguoiDung()
        Dim dsNhomNd As BindingList(Of NhomNDDTO) = NhomNDDAO.layDsNhomND()
        For Each nd As NguoiDungDTO In dsND
            Dim nhom As NhomNDDTO = dsNhomNd.ToList().Find(Function(values As NhomNDDTO)
                                                               Return values.MaNhom = nd.MaNhom
                                                           End Function)
            nhom.dsNguoiDung.Add(nd)

        Next
        'For Each nhom As NhomNDDTO In dsNhomNd
        '    nhom.dsNguoiDung.Add(dsND.ToList().Find(Function(value As NguoiDungDTO)
        '                                                Return value.MaNhom = nhom.MaNhom
        '                                            End Function))
        'Next
        Return dsNhomNd
    End Function

    Shared Function layDsVaiTro() As DataTable
        Return NhomNDDAO.layDsVaiTro()
    End Function

    Shared Function themNhom(nhom As DTO.NhomNDDTO, treePhanQuyen As TreeList, maNhom As String) As String
        Dim kq As String = NhomNDDAO.themNhom(nhom)
        dsQuyenNhomND = New List(Of PhanQuyenNhonNDDTO)
        For Each node As TreeListNode In treePhanQuyen.Nodes
            layDsQuyen(node, maNhom)
        Next
        If kq <> "-1" Then
            NhomNDDAO.phanQuyenChoNhom(dsQuyenNhomND, kq)
        Else
            kq = "-1"
        End If
        Return kq
    End Function

    Private Shared Sub layDsQuyen(node As TreeListNode, maNhom As String)
        Dim pq As New PhanQuyenNhonNDDTO
        pq.MaNhom = maNhom
        pq.MaChucNang = Integer.Parse(node.Id.ToString) + 1
        pq.TatCa = Boolean.Parse(node.GetValue(1).ToString)
        pq.TruyCap = Boolean.Parse(node.GetValue(2).ToString)
        pq.Them = Boolean.Parse(node.GetValue(3).ToString)
        pq.Sua = Boolean.Parse(node.GetValue(4).ToString)
        pq.Xoa = Boolean.Parse(node.GetValue(5).ToString)
        pq.InPQ = Boolean.Parse(node.GetValue(6).ToString)
        pq.Nhap = Boolean.Parse(node.GetValue(7).ToString)
        pq.Xuat = Boolean.Parse(node.GetValue(8).ToString)
        dsQuyenNhomND.Add(pq)
        For Each nodeChild As TreeListNode In node.Nodes
            If node.HasChildren Then
                layDsQuyen(nodeChild, maNhom)
            End If
        Next
    End Sub

    Shared Function layDsNhomNDBangMa(maNhom As String) As NhomNDDTO
        Return NhomNDDAO.layDsNhomNDBangMa(maNhom)
    End Function

    Shared Function capNhatNhom(nhom As NhomNDDTO, treeList As TreeList, maNhom As String) As Integer
        Dim kq As Integer = NhomNDDAO.CapNhatNhom(nhom)
        dsQuyenNhomND = New List(Of PhanQuyenNhonNDDTO)
        For Each node As TreeListNode In treeList.Nodes
            layDsQuyen(node, maNhom)
        Next
        NhomNDDAO.CapNhatPhanQuyen(dsQuyenNhomND, nhom.MaNhom)
        Return kq
    End Function


End Class
